package com.derbysoft.dswitch.adapter.ihg.common.utils;

import org.apache.commons.lang.time.FastDateFormat;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public abstract class DateUtils {

    private static final long MILLISECONDS_IN_DAY = 24 * 60 * 60 * 1000;

    public static final FastDateFormat FAST_DAY_FORMATTER = FastDateFormat.getInstance("yyyy-MM-dd");

    public static Date getDate(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, day);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    public static Date parseUseDefaultFormat(String date) {
        try {
            return getDayFormatter().parse(date);
        } catch (ParseException e) {
            throw new RuntimeException("parse date [" + date
                    + "] failed in use [" + getDayFormatter() + "]", e);
        }
    }

    public static Date parse(String date, DateFormat dateFormat) {
        try {
            return dateFormat.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException("parse date [" + date + "] failed in use [" + dateFormat + "]", e);
        }
    }

    public static Date parseUseFormatMMYY(String mmyy) {
        try {
            return getMMYYFormatter().parse(mmyy);
        } catch (ParseException e) {
            throw new RuntimeException("parse date [" + mmyy
                    + "] failed in use [" + getMMYYFormatter() + "]", e);
        }
    }

    private static SimpleDateFormat getMMYYFormatter() {
        return new SimpleDateFormat("MMyy");
    }

    private static SimpleDateFormat getDayFormatter() {
        return new SimpleDateFormat("yyyy-MM-dd");
    }

    public static String format(Date date, DateFormat df) {
        if (date == null) {
            return "";
        } else if (df != null) {
            return df.format(date).toLowerCase();
        } else {
            return FAST_DAY_FORMATTER.format(date);
        }
    }

    public static String format(Date date) {
        return format(date, null);
    }

    public static String formateUseFormatMMYY(Date date) {
        return format(date, getMMYYFormatter());
    }

    public static int getDayRangeBetween2Date(Date startDate, Date endDate) {
        return (int) ((endDate.getTime() - startDate.getTime()) / MILLISECONDS_IN_DAY);
    }

    public static int getDayRangeBetween2Date(String startDate, String endDate) {
        return (int) ((parseUseDefaultFormat(endDate).getTime() - parseUseDefaultFormat(startDate).getTime()) / MILLISECONDS_IN_DAY);
    }

    public static Date prev(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, -1);
        return cal.getTime();
    }

    public static Date next(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, 1);
        return cal.getTime();
    }

    public static String prev(String date) {
        return format(prev(parseUseDefaultFormat(date)));
    }

    public static String next(String date) {
        return format(next(parseUseDefaultFormat(date)));
    }
}
